#!/bin/bash
# Created Time:    2016-02-27 20:48:46
# Modified Time:   2016-02-28 15:47:16

#这告诉bash一但有任何一个语句返回非真的值，则退出bash。
set -e

#DEBUG=1

USERDIR=$(cd ~; pwd)
PROG=`basename ${BASH_SOURCE[0]}`

function log {
    if [[ -n $DEBUG ]] ; then
        for ARG in "$@"
        do
            echo "$ARG"
        done
    fi
}

log "Debug模式"

function print_help {
    echo -e "Usage: $PROG [options] [path]"
    echo -e ""
    echo -e "Options:"
    echo -e "  -e EMAIL, --email=EMAIL"
    echo -e "    User email."
    echo -e ""
    echo -e "  -n NAME, --name=NAME"
    echo -e "    User name."
    echo -e ""
    echo -e "  --help"
    echo -e "    Displays help message"
    echo -e ""
}

CMD_GETOPT=`getopt -o e:hn: --long email:,help,name: -n 'example.bash' -- "$@"`
CMD_GETOPT_RET=$?
if [ $CMD_GETOPT_RET -ne 0 ] ; then
    exit $CMD_GETOPT_RET
fi
eval set -- "$CMD_GETOPT"

while true ; do
    case "$1" in
        -e|--email) USER_EMAIL="$2" ; shift 2 ;;
        -n|--name) USER_NAME="$2" ; shift 2 ;;
        --help) print_help; exit 1 ;;
        --) shift; break ;;
        *) echo "Unknown option: $1"; print_help; exit 1 ;;
    esac
done

if [[ -z $USER_EMAIL ]] ; then
    echo "User email is empty."
    exit 1
fi
if [[ -z $USER_NAME ]] ; then
    echo "User name is empty."
    exit 1
fi
log $USER_EMAIL $USER_NAME

git config --global user.name $USER_NAME
git config --global user.email $USER_EMAIL
git config --global core.editor "vim"
git config --global color.ui "true"
#git config --global http.proxy http://127.0.0.1:3232
git config --global http.SSLVERIFY "false"
git config --global alias.lg "log --graph --stat --topo-order --color-words --decorate --pretty=format:'%C(bold yellow)%H %C(reset)%C(green)%s%d%n%C(bold red)%an%C(reset)<%ae> modified %C(cyan)%ar %C(reset)(%ai)%n%C(bold magenta)%cn%C(reset)<%ce> committed %C(cyan)%cr %C(reset)(%ci)%n'"

if [ ! -d "$USERDIR/.vim_config" ] ; then
    git clone https://git.oschina.net/vic.liu/vim_config.git ~/.vim_config
    bash ~/.vim_config/install.sh
fi

if [ ! -f "$USERDIR/.ssh/id_rsa" ] ; then
    ssh-keygen -t rsa -b 4096 -C $USER_EMAIL
fi
